package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.R
import com.sy.simpleegg.bean.WithdrawalGoodBean
import com.sy.simpleegg.config.Config
import com.sy.simpleegg.constant.ProductType
import com.sy.simpleegg.databinding.ItemWithdrawalGoodBinding
import com.sy.simpleegg.ext.onClick

class WithdrawalGoodAdapter(val context: Context, val withdrawalGoods: MutableList<WithdrawalGoodBean>, val itemListener: ItemListener):
    RecyclerView.Adapter<WithdrawalGoodAdapter.ViewHolder>() {
        
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WithdrawalGoodAdapter.ViewHolder {
        val binding = ItemWithdrawalGoodBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return withdrawalGoods.size
    }

    @SuppressLint("RecyclerView", "SetTextI18n")
    override fun onBindViewHolder(holder: WithdrawalGoodAdapter.ViewHolder, position: Int) {
        val item = withdrawalGoods[position]
        holder.itemName.text = "${item.value}" +
                when (Config.PRODUCT_TYPE){
                    ProductType.CURRENCY -> context.getString(R.string.bi)
                    ProductType.BEAD -> context.getString(R.string.bead)
                }

        holder.itemLayout.onClick {
            itemListener.onClick(item.value)
        }
    }

    interface ItemListener{
        fun onClick(quantity: Int)
    }

    inner class ViewHolder(binding: ItemWithdrawalGoodBinding): RecyclerView.ViewHolder(binding.root){
        val itemLayout: RelativeLayout = binding.rlItem
        val itemName: TextView = binding.tvCoins
    }
}